package com.nbgs.demo.controller;

import com.nbgs.demo.common.Result;
import com.nbgs.demo.controller.form.Form;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

//@CrossOrigin("*")
@RestController
@Slf4j
public class LoginController {
    @PostMapping(value = "/login")
//    public Result login(Form loginForm, HttpSession session){
    public Result login(Form loginForm){
        log.info("登录:{}",loginForm);
//        session.setAttribute("workerNum","01");
        System.out.println(loginForm.getWorkerNum());
        System.out.println("111");
        System.out.println(loginForm.getWorkerPassword());

        try{
            Subject subject = SecurityUtils.getSubject();
            subject.login(new UsernamePasswordToken(loginForm.getWorkerNum(),loginForm.getWorkerPassword()));
        }catch (AuthenticationException e){
            log.info("登录失败",e);
            return Result.fail("登录失败，用户名或者密码不正确");
        }
        return Result.success();
    }
}